/*
 * @Author: wangding
 * @Date: 2020-11-09 15:32:40
 * @LastEditTime: 2021-05-10 15:08:25
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\service\comm.js
 */
import Vue from 'vue';
import { Message } from 'element-ui';
Vue.prototype.$message = Message;
import i18n from '../lan'
Vue.component(Message)
export default {
    floatNum(val) {
        if (isNaN(val)) {
            return '-'
        } else {
            if (val) {
                return Number(val).toFixed(2)
            } else {
                // return '0.00'
                return '0'
            }
        }
    },
    returnFlag(val) {
        let permission = sessionStorage.getItem('role') || '1000'
        let arr = permission.split('')
        if (arr[val] == 1) {
            return true
        } else {
            return false
        }
    },
    returnFlagAd(val) {
        let permission = sessionStorage.getItem('roleAd') || '111'
        let arr = permission.split('')
        if (arr[val] == 1) {
            return true
        } else {
            return false
        }
    },
    returnCode(data, val, valueText, code) {
        if (!valueText) {
            valueText = 'value'
        }
        if (!code) {
            code = 'code'
        }
        let name = ''
        if (!data) {
            return
        }
        data.forEach(item => {
            if (item[valueText] == val) {
                name = item[code]
            }
        })
        return name
    },
    showMsg(flag, Msg) {
        // console.log(Vue.prototype.$message, 'Vue');
        if (flag) {
            Vue.prototype.$message({
                message: Msg || i18n.t('index.control_success'),
                type: 'success'
            });
        } else {
            Vue.prototype.$message({
                message: Msg || i18n.t('index.control_fail'),
                type: 'warning'
            });
        }
        return flag
    },
    windowOpen(url, params) {
        let URL = ''
        if (url.indexOf('?') > 0) {
            URL = url + '&token=' + sessionStorage.getItem('token') + '&lang=' + localStorage.getItem('lang')
        } else {
            URL = url + '?token=' + sessionStorage.getItem('token') + '&lang=' + localStorage.getItem('lang')
        }
        if (params) {
            var str = [];
            for (var key in params) {
                str.push(key + "=" + params[key]);
            }
            URL += str.length > 0 ? '&' + str.join('&') : ''
        }
        window.open(process.env.VUE_APP_DOWNLOAD_PROXY + URL)
    }

}